package com.kermel.andromeda.data;

import com.kermel.andromeda.R;
import com.kermel.common.util.StringUtils;

import android.content.Context;
import android.test.AndroidTestCase;

public class WhenCountdownTimerEndsChoiceTest extends AndroidTestCase {
    public static final String DISPLAY_NAME_FIXTURE = "displayNameFixture";
    
    private WhenCountdownTimerEndsChoice choice;
    
    protected void setUp() throws Exception {
        super.setUp();
        
        choice = WhenCountdownTimerEndsChoice.UNKNOWN;
    }

    protected void tearDown() throws Exception {
        choice = WhenCountdownTimerEndsChoice.UNKNOWN;
        
        super.tearDown();
    }

    public void testIsUnknown() {
        assertTrue(choice.isUnknown());
    }

    public void testIsNotUnknown() {
        choice = WhenCountdownTimerEndsChoice.PLAY_SOUND;
        assertTrue(choice.isNotUnknown());
    }
    
    public void testIsPlaySound() {
        choice = WhenCountdownTimerEndsChoice.PLAY_SOUND;
        assertTrue(choice.isPlaySound());
    }

    public void testIsNotPlaySound() {
        assertTrue(choice.isNotPlaySound());
    }
    
    public void testGetLocalizedDisplayNameForUnknown() {
        int resID = choice.getLocalizedDisplayNameResID();
        int expectedResID = R.string.unknown;
        
        performLocalizedDisplayNameTest(resID, expectedResID);
    }
    
    public void testGetLocalizedDisplayNameForPlaySound() {
        choice = WhenCountdownTimerEndsChoice.PLAY_SOUND;
        int resID = choice.getLocalizedDisplayNameResID();
        int expectedResID = R.string.playSound;
        
        performLocalizedDisplayNameTest(resID, expectedResID);
    }
    
    private void performLocalizedDisplayNameTest(int resID, int expectedResID) {
        assertFalse(resID == 0);
        
        String expected = getContext().getString(expectedResID);
        assertNotNull(expected);
        
        String actual = getContext().getString(resID);
        assertNotNull(actual);
        
        assertEquals(expected, actual);
    }
    
    public void testThatGetAllKnownValuesDoesNotReturnUnknown() {
        WhenCountdownTimerEndsChoice[] choices = WhenCountdownTimerEndsChoice.getAllValuesExceptUnknown();
        
        assertNotNull(choices);
        assertTrue(choices.length > 0);
        
        boolean result = containsUnknown(choices);
        assertFalse(result);
    }
    
    private boolean containsUnknown(WhenCountdownTimerEndsChoice[] choices) {
        boolean result = false;
        
        for (WhenCountdownTimerEndsChoice choice : choices) {
            if (choice.isUnknown())
                return true;
        }
        
        return result;
    }
    
    public void testThatGetChoiceByLocalizedDisplayNameResIDReturnsUnknown() {
        int resID = 0;
        WhenCountdownTimerEndsChoice choice = WhenCountdownTimerEndsChoice.getChoiceByLocalizedDisplayNameResID(resID);
        
        assertNotNull(choice);
        assertTrue(choice.isUnknown());
    }
    
    public void testThatGetChoiceByLocalizedDisplayNameResIDReturnsTheExpectedValue() {
        WhenCountdownTimerEndsChoice choiceFixture = WhenCountdownTimerEndsChoice.PLAY_SOUND;
        int resID = choiceFixture.getLocalizedDisplayNameResID();
        
        WhenCountdownTimerEndsChoice choice = WhenCountdownTimerEndsChoice.getChoiceByLocalizedDisplayNameResID(resID);
        assertEquals(choiceFixture, choice);
    }
    
    public void testThatGetChoiceByLocalizedDisplayNameReturnsUnknownWhenContextIsNull() {
        Context context = null;
        String displayName = DISPLAY_NAME_FIXTURE;
        WhenCountdownTimerEndsChoice choice = WhenCountdownTimerEndsChoice.getChoiceByLocalizedDisplayName(context, displayName);
        assertTrue(choice.isUnknown());
    }
    
    public void testThatGetChoiceByLocalizedDisplayNameReturnsUnknownWhenDisplayNameIsNull() {
        Context context = getContext();
        String displayName = null;
        WhenCountdownTimerEndsChoice choice = WhenCountdownTimerEndsChoice.getChoiceByLocalizedDisplayName(context, displayName);
        assertTrue(choice.isUnknown());
    }
    
    public void testThatGetChoiceByLocalizedDisplayNameReturnsUnknownWhenDisplayNameIsEmpty() {
        Context context = getContext();
        String displayName = StringUtils.EMPTY;
        WhenCountdownTimerEndsChoice choice = WhenCountdownTimerEndsChoice.getChoiceByLocalizedDisplayName(context, displayName);
        assertTrue(choice.isUnknown());
    }
    
    public void testThatGetChoiceByLocalizedDisplayNameReturnsUnknownWhenDisplayNameDoesNotMatchAnyOfTheValues() {
        Context context = getContext();
        String displayName = DISPLAY_NAME_FIXTURE;
        WhenCountdownTimerEndsChoice choice = WhenCountdownTimerEndsChoice.getChoiceByLocalizedDisplayName(context, displayName);
        assertTrue(choice.isUnknown());
    }
    
    public void testThatGetChoiceByLocalizedDisplayNameReturnsTheExpectedValue() {
        WhenCountdownTimerEndsChoice choiceFixture = WhenCountdownTimerEndsChoice.PLAY_SOUND;
        int resID = choiceFixture.getLocalizedDisplayNameResID();
        Context context = getContext();
        String displayName = getContext().getString(resID);
        
        WhenCountdownTimerEndsChoice choice = WhenCountdownTimerEndsChoice.getChoiceByLocalizedDisplayName(context, displayName);
        assertEquals(choiceFixture, choice);
    }

}
